const Mock = require("mockjs");
import { builder, getBody } from "./util";
const loginResponseSuccess = {
  code: 200,
  msg: "success",
  data: {
    userinfo: {
      username: "lizheng",
      avator:
        "https://dss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=1055388296,1425226147&fm=26&gp=0.jpg"
    },
    token: ""
  },
  timestamps: new Date().getTime()
};

const menuListResponse = {
  code: 200,
  msg: "success",
  data: [
    {
      key: "1",
      icon: "pie-chart",
      name: "一级菜单",
      children: [
        {
          key: "1-1",
          icon: null,
          name: "二级菜单",
          children: null
        }
      ]
    },
    {
      key: "2",
      icon: "pie-chart",
      name: "一级菜单",
      children: [
        {
          key: "2-1",
          icon: null,
          name: "二级菜单",
          children: null
        }
      ]
    },
    {
      key: "3",
      icon: "pie-chart",
      name: "一级菜单",
      children: null
    }
  ],
  timestamps: new Date().getTime()
};

// Mock.mock( url, post/get , 返回的数据)；

Mock.mock("/api/login", "post", options => {
  const data = getBody(options);
  const checkcode = sessionStorage.getItem("checkcode");
  if (data.checkcode.toLowerCase() !== checkcode.toLowerCase()) {
    return loginResponseFail("验证码错误!");
  }
  if (data.username === "admin" && data.password === "lz123456.") {
    sessionStorage.removeItem("checkcode");
    return loginResponseSuccess;
  } else {
    return loginResponseFail();
  }
});
Mock.mock("/api/menuList", "post", menuListResponse);
Mock.mock("/api/checkcode", "post", options => {
  return builder(Random());
});

function loginResponseFail(msg) {
  return {
    code: 500,
    msg: msg || "用户名或密码有误！",
    data: null,
    timestamps: new Date().getTime()
  };
}
function Random() {
  const checkcode = Mock.mock({ regexp: /\w{4}/ }).regexp;
  console.log(checkcode);
  sessionStorage.setItem("checkcode", checkcode);
  return Mock.Random.dataImage("120x30", checkcode);
}
